// pages/admin/index.js
const app = getApp()
const serverHost = app.config.serverHost
Page({

  data: {
    active: 0,
    uploaderList: [],
    uplist:[],
    imgindex: 0,
    uploaderNum: 0,
    showUpload: true,
    uploaderList1: [],
    uplist1: [],
    imgindex1: 0,
    uploaderNum1: 0,
    showUpload1: true,
    title_value: '',
    bz_value: '',
    guige_value: '',
    danwei_value: '',
    shuliang_value: '',
    gongyingshang_value: '',
    yuanjia_value: '',
    xianjia_value: '',
    fencheng_value:'',
    jieshao_value: '',
    array: ['时令水果', '新鲜蔬菜', '肉禽蛋类', '海鲜水产', '酒水饮料', '零食熟食', '粮油速食', '纸品清洁'],
    objectArray: [{ id: 0, name: '时令水果' }, { id: 1, name: '新鲜蔬菜' }, { id: 2, name: '肉禽蛋类' }, { id: 3, name: '海鲜水产' }, { id: 4, name: '酒水饮料' }, { id: 5, name: '零食熟食' }, { id: 6, name: '粮油速食' }, { id: 7, name: '纸品清洁' }],
    index: 0,
    array1: ['所有区域', '香洲区', '金湾区', '斗门区'],
    objectArray: [{ id: 0, name: '所有区域' }, { id: 1, name: '香洲区' }, { id: 2, name: '金湾区' }, { id: 3, name: '斗门区' }],
    index1: 0
  },
  // 商品分类
  bindPickerChange: function (e) {
    this.setData({
      index: e.detail.value
    })
  },
  // 销售区域
  bindPickerChange1: function (e) {
    this.setData({
      index1: e.detail.value
    })
  },
  //商品标题
  gettitle_value: function (event) {
    this.setData({
      title_value: event.detail.value
    })
  },
// 商品备注
  getbz_value: function (event) {
    this.setData({
      bz_value: event.detail.value
    })
  },
// 商品规格
  getguige_value: function (event) {
    this.setData({
      guige_value: event.detail.value
    })
  },
// 商品单位
  getdanwei_value: function (event) {
    this.setData({
      danwei_value: event.detail.value
    })
  },
  // 总数量
  getgongyingshang_value: function (event) {
    this.setData({
      gongyingshang_value: event.detail.value
    })
  },

  // 现价
  getyuanjia_value: function (event) {
    this.setData({
      yuanjia_value: event.detail.value
    })
  },
  // 原价
  getxianjia_value: function (event) {
    this.setData({
      xianjia_value: event.detail.value
    })
  },
  // 商品分成
  getfencheng_value(e){
    this.setData({
      fencheng_value:e.detail.value
    })
  },
  // 商品介绍
  getjieshao_value(event){
    this.setData({
      jieshao_value:event.detail.value
    })
  },
  // 删除图片
  clearImg: function (e) {
    var nowList = [];//新数据
    var uploaderList = this.data.uploaderList;//原数据
    for (let i = 0; i < uploaderList.length; i++) {
      console.log(e)
      if (i == e.currentTarget.dataset.index) {
        continue;
      } else {
        nowList.push(uploaderList[i])
      }
    }
    this.setData({
      uploaderNum: this.data.uploaderNum - 1,
      uploaderList: nowList,
      showUpload: true
    })
  },
  //展示图片
  showImg: function (e) {
    var that = this;
    wx.previewImage({
      urls: that.data.uploaderList,
      current: that.data.uploaderList[e.currentTarget.dataset.imgindex]
    })
  },
  //上传图片
  upload: function (e) {
    var that = this;
    wx.chooseImage({
      count: 5 - that.data.uploaderNum, // 默认9
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        console.log(res)
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        let tempFilePaths = res.tempFilePaths;
        let uploaderList = that.data.uploaderList.concat(tempFilePaths);
        if (uploaderList.length == 5) {
          that.setData({
            showUpload: false
          })
        }
        that.setData({
          uploaderList: uploaderList,
          uploaderNum: uploaderList.length,
        })
      }
    })
  },
  // 删除图片1
  clearImg1: function (e) {
    var nowList = [];//新数据
    var uploaderList = this.data.uploaderList1;//原数据
    for (let i = 0; i < uploaderList.length; i++) {
      if (i == e.currentTarget.dataset.index) {
        continue;
      } else {
        nowList.push(uploaderList[i])
      }
    }
    this.setData({
      uploaderNum1: this.data.uploaderNum1 - 1,
      uploaderList1: nowList,
      showUpload1: true
    })
  },
  //展示图片1
  showImg1: function (e) {
    var that = this;
    wx.previewImage({
      urls: that.data.uploaderList1,
      current: that.data.uploaderList1[e.currentTarget.dataset.index]
    })
  },
  //上传图片1
  upload1: function (e) {
    var that = this;
    wx.chooseImage({
      count: 5 - that.data.uploaderNum1, // 默认9
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        console.log(res)
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        let tempFilePaths = res.tempFilePaths;
        let uploaderList = that.data.uploaderList1.concat(tempFilePaths);
        if (uploaderList.length == 5) {
          that.setData({
            showUpload1: false
          })
        }
        that.setData({
          uploaderList1: uploaderList,
          uploaderNum1: uploaderList.length,
        })
      }
    })
  },
  onChange(event) {
    if (event.detail == "1") {
      wx.redirectTo({
        url: '../admin/Goodslist/index',
      })
    } else if (event.detail == "2") {
      wx.redirectTo({
        url: '../admin/order/order',
      })
    } else if (event.detail == "3") {
      wx.redirectTo({
        url: '../admin/my/my',
      })
    }
  },
  //上传信息
  async updata() {
    var phone = wx.getStorageSync('phone');
    if (!this.data.title_value || !this.data.bz_value || !this.data.guige_value || !this.data.danwei_value || !this.data.yuanjia_value || !this.data.xianjia_value ||!this.data.fencheng_value || !this.data.jieshao_value) {
      wx.showToast({
        title: '请完善商品信息',
        icon: 'none',
        duration: 1500
      })
    }else if(this.data.fencheng_value > this.data.yuanjia_value){
      wx.showToast({
        title: '分成不能大于现价，请重新输入',
        icon: 'none',
        duration: 1500,
        mask: false
      })
    }else{
      var that = this;
      wx.showModal({
        title: '上传商品',
        content: '确认上传？',
        async success(res) {
          if (res.confirm) {
      if (that.data.uploaderList.length != 0 || that.data.uploaderList1.length != 0) {
        wx.showLoading({
          title: '加载中',
        })
    for (var i = 0; i < that.data.uploaderList.length; i++) {
      console.log(that.data.uploaderList[i])
      var datass = that.data.uploaderList[i]
      await   wx.uploadFile({
        url: serverHost+'/api/admin/upload', //仅为示例，非真实的接口地址
        filePath: datass,
        name: 'file',
        formData: {
          'user': 'test'
        },
        success(res) {
          const datas = res.data
          that.data.uplist.push(datas)
        }
      })
    }
        console.log(that.data.uplist)
      for (var i = 0; i < that.data.uploaderList1.length; i++) { 
      console.log(that.data.uploaderList1[i])
      var datass = that.data.uploaderList1[i]
     await wx.uploadFile({
       url: serverHost + '/api/admin/upload', //仅为示例，非真实的接口地址
        filePath: datass,
        name: 'file',
        formData: {
          'user': 'test'
        },
        success(res) {
          const datas = res.data
          console.log(datas)
          that.data.uplist1.push(datas)
        }
      })
    }
        console.log(that.data.uplist1)
        var updatas = {
          name: that.data.title_value,
          desc: that.data.bz_value,
          specifications: that.data.guige_value,
          unit: that.data.danwei_value,
          price: that.data.yuanjia_value,
          oprice: that.data.xianjia_value,
          fencheng:that.data.fencheng_value,
          goods_detail:that.data.jieshao_value,
          classes: that.data.array[that.data.index],
          area: that.data.array1[that.data.index1],
          goods_imgUrl: that.data.uplist,
          detail_imgUrl: that.data.uplist1,
          phone:phone,
          number: that.data.gongyingshang_value
        }
        setTimeout(function () {
          that.updatass(updatas);
        }, 2000)
    }else{
        wx.hideLoading();
        wx.showToast({
          title: '请上传商品图片',
          icon: 'none',
          duration: 1500
        });
    }



          } else {
            console.log('点击取消回调')
          }
        }
      })
   }
  } ,
  async updatass(updatas){
    if (this.data.uplist1 == '' || this.data.uplist == '') {
      wx.showToast({
        title: '上传失败，请重试',
        icon: '',     //默认值是success,就算没有icon这个值，就算有其他值最终也显示success
        duration: 1000,      //停留时间
      })
      wx.hideLoading();
  } else {
    await app.api.post("admin/goodslist", updatas, (res) => {
      console.log(res)
    })
      this.setData({
      uplist: [],
      uplist1: [],
        uploaderList1:[],
        uploaderList:[],
        title_value:'',
        bz_value: '',
        guige_value: '',
        danwei_value: '',
        shuliang_value: '',
        gongyingshang_value: '',
        yuanjia_value: '',
        xianjia_value: '',
        fencheng_value: '',
        jieshao_value: '',
    })
      wx.hideLoading();

      wx.showToast({
        title: '上传成功',
        icon: '',     //默认值是success,就算没有icon这个值，就算有其他值最终也显示success
        duration: 1000,      //停留时间
      })
  }
}
})